package org.yxing.palpalgps.http;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;

public class Download {
    /**
     * 下载图片
     * @param imgSrc
     * @return Bitmap
     */
    public static Bitmap loadImgBmp(String imgSrc) {
        Bitmap bmp = null;
        
        try {
            URL url = new URL(imgSrc);
            InputStream is = (InputStream) url.getContent();
            bmp = BitmapFactory.decodeStream(is);
            is.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        return bmp;
    }
    
    /**
     * Download image
     * @param imgSrc
     * @return Drawable obj
     */
    public static Drawable loadimgDrawable(String imgSrc) {
        Drawable drawable = null;
        
        try {
            URL url = new URL(imgSrc);
            InputStream is = (InputStream) url.getContent();
            drawable = Drawable.createFromStream(is, "src");
            if (is != null)
                is.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        return drawable;
    }
}
